package com.diligent.cloud.sfc.service.impl;

import com.canny.commons.model.ListResult;
import com.canny.commons.model.ObjectResult;
import com.diligent.cloud.sfc.model.MfgConsume;
import com.diligent.cloud.sfc.model.OrderType;
import com.diligent.cloud.sfc.model.info.inventory.MatInventoryInfo;
import com.diligent.cloud.sfc.model.params.inventory.RubberStripInventoryParams;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.sfc.service.WorkShopPostProcessService;
import com.diligent.cloud.workordermd.feign.service.MfgConsumeDetailFeignService;
import com.diligent.cloud.workordermd.feign.service.MfgConsumeMasterFeignService;
import com.diligent.cloud.workordermd.model.MfgConsumeDetail;
import com.diligent.cloud.workordermd.model.MfgConsumeMaster;
import org.springframework.stereotype.Service;

import java.security.Principal;

/**
 * WorkShopPostProcessServiceImpl class
 *
 * @author xf107278
 * @date 4/20/2020
 */
@Service
public class WorkShopPostProcessServiceImpl implements WorkShopPostProcessService {
    private ISfcService sfcService;

    private MfgConsumeMasterFeignService mfgConsumeMasterFeignService;

    private MfgConsumeDetailFeignService mfgConsumeDetailFeignService;



    public WorkShopPostProcessServiceImpl(ISfcService sfcService,
                                          MfgConsumeMasterFeignService mfgConsumeMasterFeignService,
                                          MfgConsumeDetailFeignService mfgConsumeDetailFeignService) {
        this.sfcService = sfcService;
        this.mfgConsumeMasterFeignService = mfgConsumeMasterFeignService;
        this.mfgConsumeDetailFeignService = mfgConsumeDetailFeignService;

    }

    @Override
    public ListResult getRubberStripList(String machCode, String matCode) {
        return sfcService.getMatList(machCode, OrderType.后加工工单, matCode);
    }

    @Override
    public ObjectResult addRubberStripInventory(RubberStripInventoryParams rubberStripInventoryParams,
                                                Principal principal) {

        MfgConsumeMaster consumeMaster ;
        MfgConsumeDetail consumeDetail ;
        for (MatInventoryInfo matInventoryInfo : rubberStripInventoryParams.getRubberStripInventoryInfos()) {

            MfgConsume mfgConsume = sfcService.addMatInventoryInfo(matInventoryInfo
                    , sfcService.addMatConsumerInventory(rubberStripInventoryParams, principal));

            consumeMaster = mfgConsume.getMfgConsumeMaster();
            consumeDetail = mfgConsume.getMfgConsumeDetail();

            consumeDetail.setDigit(matInventoryInfo.getDigit());

            mfgConsumeMasterFeignService.insert(consumeMaster);
            mfgConsumeDetailFeignService.insert(consumeDetail);
        }
        return new ObjectResult("");
    }


}
